class Solution
{
public:
    int LastRemaining_Solution(int n, int m)
    {
        if (n == 0)  return -1;
        int j = 0;
        for (int i = 2; i <= n; i++)
            j = (j + m) % i;
        return j;
    }
};